from typing import *
from sortedcontainers import SortedList


class TaskManager:

    def __init__(self, tasks: List[List[int]]):
        self.task_user = dict()
        self.task_priority = dict()
        self.priority_task = SortedList()
        for userId, taskId, priority in tasks:
            self.task_user[taskId] = [userId, priority]
            self.priority_task.add((-priority, -taskId))

    def add(self, userId: int, taskId: int, priority: int) -> None:
        self.task_user[taskId] = [userId, priority]
        self.priority_task.add((-priority, -taskId))

    def edit(self, taskId: int, newPriority: int) -> None:
        u, p = self.task_user[taskId]
        self.priority_task.pop(self.priority_task.bisect_left((-p, -taskId)))
        self.task_user[taskId] = [u, newPriority]
        self.priority_task.add((-newPriority, -taskId))

    def rmv(self, taskId: int) -> None:
        self.task_user.pop(taskId)

    def execTop(self) -> int:
        ans = -1
        while self.priority_task and ans == -1:
            p, t = self.priority_task.pop(0)
            if -t in self.task_user and self.task_user[-t][1] == -p:
                ans = self.task_user[-t][0]
        return ans


class TaskManager:

    def __init__(self, tasks: List[List[int]]):
        self.task_user = dict()
        self.task_priority = dict()
        self.priority_task = SortedList()
        for userId, taskId, priority in tasks:
            self.task_user[taskId] = [userId, priority]
            self.priority_task.add((-priority, -taskId))

    def add(self, userId: int, taskId: int, priority: int) -> None:
        self.task_user[taskId] = [userId, priority]
        self.priority_task.add((-priority, -taskId))

    def edit(self, taskId: int, newPriority: int) -> None:
        u, p = self.task_user[taskId]
        # self.priority_task.pop(self.priority_task.bisect_left((-p, -taskId)))
        self.task_user[taskId] = [u, newPriority]
        self.priority_task.add((-newPriority, -taskId))

    def rmv(self, taskId: int) -> None:
        self.task_user.pop(taskId)

    def execTop(self) -> int:
        ans = -1
        while self.priority_task and ans == -1:
            p, t = self.priority_task.pop(0)
            if -t in self.task_user and self.task_user[-t][1] == -p:
                ans = self.task_user[-t][0]
        return ans


sd = SortedList()
sd[(-20, 10)] = 5
sd[(-20, -10)] = 100
print(sd)
print(sd.popitem(0))
